'use strict';

const path = require('path');
const egg = require('egg');
const EGG_PATH = Symbol.for('egg#eggPath');
const EGG_LOADER = Symbol.for('egg#loader');
const { restful } = require('./restful');

// 自定义 AppWorkerLoader
class CustomAppWorkerLoader extends egg.AppWorkerLoader {
  constructor(options) {
    super(options);
  }

  load() {
    super.load();
    console.log('自定义load逻辑');
  }

  loadPlugin() {
    super.loadPlugin();
    console.log('自定义plugin加载逻辑');
  }

  loadConfig() {
    super.loadConfig();
    console.log('自定义config加载逻辑');
  }

  loadAgentExtend() {
    super.loadAgentExtend();
    console.log('自定义agent extend加载逻辑');
  }

  loadApplicationExtend() {
    super.loadApplicationExtend();
    console.log('自定义application extend加载逻辑');
  }

  loadRequestExtend() {
    super.loadRequestExtend();
    console.log('自定义request extend加载逻辑');
  }

  loadResponseExtend() {
    super.loadResponseExtend();
    console.log('自定义response extend加载逻辑');
  }

  loadContextExtend() {
    super.loadContextExtend();
    console.log('自定义context extend加载逻辑');
  }

  loadHelperExtend() {
    super.loadHelperExtend();
    console.log('自定义helper extend加载逻辑');
  }

  loadCustomAgent() {
    super.loadCustomAgent();
    console.log('自定义custom agent加载逻辑');
  }

  loadCustomApp() {
    super.loadCustomApp();
    console.log('自定义custom app加载逻辑');
  }

  loadService() {
    super.loadService();
    console.log('自定义service加载逻辑');
  }

  loadMiddleware() {
    super.loadMiddleware();
    console.log('自定义middleware加载逻辑');
  }

  loadController() {
    super.loadController();
    console.log('自定义controller加载逻辑');
  }

  loadRouter() {
    this.app.router.restful = restful; // 必须在加载 router 之前，挂载 restful
    super.loadRouter();
    console.log('自定义router加载逻辑');
  }
}

// 自定义 Application
class Application extends egg.Application {
  get [EGG_PATH]() {
    return path.dirname(__dirname);
  }

  get [EGG_LOADER]() {
    return CustomAppWorkerLoader;
  }
}

// 自定义 Agent
class Agent extends egg.Agent {
  get [EGG_PATH]() {
    return path.dirname(__dirname);
  }
}

module.exports = Object.assign(egg, {
  Application,
  Agent,
  AppWorkerLoader: CustomAppWorkerLoader,
});
